package playsManager.gui;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import playsManager.domainLogic.DomainLogic;

abstract class UserMenu implements IMenu{

	protected JFrame menuFrame;
	protected DomainLogic dl;
	
	protected abstract void InitMenu(); 
	
	public UserMenu(DomainLogic theDl)
	{
		menuFrame = new JFrame();
		menuFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		dl = theDl;
		
		InitMenu();
		
		menuFrame.pack();
		menuFrame.addWindowListener(new WindowListener()
		{
			@Override
			public void windowActivated(WindowEvent arg0) {}

			@Override
			public void windowClosed(WindowEvent arg0)
			{
				fireClosed(new ActionEvent(UserMenu.this, 0, "closed"));
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {}

			@Override
			public void windowDeactivated(WindowEvent arg0) {}

			@Override
			public void windowDeiconified(WindowEvent arg0) {}

			@Override
			public void windowIconified(WindowEvent arg0) {}

			@Override
			public void windowOpened(WindowEvent arg0) {}
		});
		
	}
	
	public void Show()
	{
		menuFrame.setVisible(true);
	}
	
	private EventListenerList closeListenerList = new EventListenerList();
	
	public void addCloseListener(ActionListener l) {
		closeListenerList.add(ActionListener.class, l);
	}
	
	public void removeCloseListener(ActionListener l) {
		closeListenerList.remove(ActionListener.class, l);
	}
	
	  // Roughly analogous to .net OnEvent protected virtual method pattern -
	// call this method to raise the event
	protected void fireClosed(ActionEvent e) {		
	  ActionListener[] ls = closeListenerList.getListeners(ActionListener.class);
	  for (ActionListener l : ls) {
	    l.actionPerformed(e);
	  }
	}
	
}
